package com.example.demo.type;

import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;

import java.util.HashMap;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * user:likai
 * Date:2023-02-12
 * Time:9:23
 */
//统一格式返回
@ControllerAdvice
public class CommonType implements ResponseBodyAdvice {
    //需要设置为true，然后就可以重写返回的格式
    @Override
    public boolean supports(MethodParameter returnType, Class converterType) {
        return true;
    }

    @Override
    public Object beforeBodyWrite(Object body, MethodParameter returnType,
                                  MediaType selectedContentType, Class selectedConverterType,
                                  ServerHttpRequest request, ServerHttpResponse response) {
        HashMap<String,Object> map=new HashMap<>();
        map.put("state",1);
        map.put("msg","信息");
        map.put("data",body);
        return map;
    }
}
